/**
 * Copyright (C) 2008 GeoScheduler Team, as stated on <http://www.geoscheduler.org/authors>.
 * 
 * This file is part of GeoScheduler.
 * 
 * GeoScheduler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GeoScheduler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with GeoScheduler. If not, see <http://www.gnu.org/licenses/>.
 */

package org.geoscheduler.rest;

import java.util.List;

import javax.annotation.Resource;
import javax.ws.rs.ConsumeMime;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.ProduceMime;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.geoscheduler.commons.marshaller.MarshallingIterator;
import org.geoscheduler.model.dto.TagQueryDTO;
import org.geoscheduler.rest.utils.MIMEConstants;
import org.json.JSONObject;

/**
 * 
 * <h2>TagsResource</h2>
 *
 * @author lko
 */
@Resource
@Path("tags")
public class TagsResource extends AbstractResource {

	private final static Log LOG = LogFactory.getLog(TagsResource.class);

	@POST
	@ConsumeMime(MIMEConstants.APP_JSON)
	@ProduceMime(MIMEConstants.APP_JSON)
	public JSONObject queryTags(JSONObject jsonQueryDTO) {
		try {
			TagQueryDTO queryDTO = getMarshaller().unmarshall(new TagQueryDTO(), jsonQueryDTO);
			List<TagQueryDTO.Result> ret = getTagDAO().query(queryDTO);
			return response(new MarshallingIterator<TagQueryDTO.Result>(ret.iterator(), getMarshaller()));
		} catch (Exception ex) {
			LOG.error("failed to query tags " + jsonQueryDTO, ex);
			return response(ex);
		}
	}
}
